## 9.4. Examples of Searching

When used with grep or egrep, regular expressions normally are surrounded by quotes to avoid interpretation by the shell. (If the pattern contains a $, you must use single quotes, as in `'`$200`'`, or escape the $, as in `"`\$200`"`.) When used with ed, vi, sed, and awk, regular expressions usually are surrounded by / (although any delimiter works). Here are some sample patterns:

Pattern | What does it match?
bag | The string bag.
^bag | "bag" at beginning of line or string.
bag$ | "bag" at end of line or string.
^bag$ | "bag" as the only text on line.
[Bb]ag | "Bag" or "bag."
b[aeiou]g | Second character is a vowel.
b[^aeiou]g | Second character is not a vowel.
b.g | Second character is any character except newline.
^...$ | Any line containing exactly three characters.
^\. | Any line that begins with a dot.
^\.[a-z][a-z] | Same, followed by two lowercase letters (e.g., troff requests).
^\.[a-z]\{2\} | Same as previous, grep or sed only.
^[^.] | Any line that doesn't begin with a dot.
bugs* | "bug," "bugs", "bugss", etc.
"word" | A word in quotes.
"*word"* | A word, with or without quotes.
[A-Z][A-Z]* | One or more uppercase letters.
[A-Z]+ | Same, egrep or awk only.
[A-Z].* | An uppercase letter, followed by zero or more characters.
[A-Z]* | Zero or more uppercase letters.
[a-zA-Z] | Any letter.
[0-9A-Za-z]+ | Any alphanumeric sequence.

egrep or awk pattern | What does it match?
--- | ---
[567] | One of the numbers 5, 6, or 7
five|six|seven | One of the words five, six, or seven
80[23]?86 | 8086, 80286, or 80386
compan(y|ies) | company or companies

vi pattern | What does it match?
--- | ---
\<the | Words like theater or the
the\> | Words like breathe or the
\<the\> | The word the

sed or grep pattern | What does it match?
--- | ---
0\{5,\} | Five or more zeros in a row
[0-9]\{3\}-[0-9]\{2\}-[0-9]\{4\} | Social security number (nnn-nn-nnnn)

### 9.4.1. Examples of Searching and Replacing

The following examples show the metacharacters available to sed and vi. We have shown vi commands with an initial colon because that is how they are invoked within vi. A space is marked by a ; a tab is marked by tab.

Command | Result
--- | ---
s/.*/(&)/ | Reproduce the entire line, but add parentheses.
s/.*/mv & &.old/ | Change a wordlist (one word per line) into mv commands.
/^$/d | Delete blank lines.
:g/^$/d | Same as previous, in vi editor.
/^[tab]*$/d | Delete blank lines, plus lines containing spaces or tabs.
:g/^[tab]*$/d | Same as previous, in vi editor.
s/*//g | Turn one or more spaces into one space.
:%s/*//g | Same as previous, in vi editor.
:s/[0-9]/Item &:/ | Turn a number into an item label (on the current line).
:s | Repeat the substitution on the first occurrence.
:& | Same as previous.
:sg | Same, but for all occurrences on the line.
:&g | Same as previous.
:%&g | Repeat the substitution globally.
:.,$s/Fortran/\U&/g | Change word to uppercase, on current line to last line.
:%s/.*/\L&/ | Lowercase entire file.
:s/\<./\u&/g | Uppercase first letter of each word on current line. (Useful for titles.)
:%s/yes/No/g | Globally change a word (yes) to another word (No).
:%s/Yes/~/g | Globally change a different word to No (previous replacement).

Finally, here are some sed examples for transposing words. A simple transposition of two words might look like this:

>     s/die or do/do or die/	Transpose words

The real trick is to use hold buffers to transpose variable patterns. For example:

>     s/\([Dd]ie\) or \([Dd]o\)/\2 or\1/	  Transpose, using hold buffers

Copyright © 2001 O'Reilly & Associates. All rights reserved.